#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<limits.h>
//1.空字符串
//2.空指针
//3.+ -
//4.非数字字符
//5.越界
enum state 
{
	VALIDE,
	INVALIDE
};

enum state s = INVALIDE;

int my_atoi(char* str)
{
	assert(str);
	int flag = 1;
	if (*str == '\0')
	{
		if (s == INVALIDE)
		{
			printf("输入非法\n");
		}
		return 0;
	}
	if (*str == '+')
	{
		flag = 1;
		str++;
	}
	else if (*str == '-')
	{
		flag = -1;
		str++;
	}
	long long ret = 0;
	while (*str)
	{
				if (isdigit(*str))
			{
				ret = ret * 10 + (*str - '0')*flag;
				if (ret > INT_MAX || ret < INT_MIN)
				{
					if (s == INVALIDE)
					{
						printf("输入非法\n");
					}
					return 0;
				}
			}
			else
			{
					if (s == INVALIDE)
					{
						printf("输入非法\n");
					}
				return ret;
			}
			str++;
	}
	if (*str == '\0')
	{
		s = VALIDE;
	}
	return (int)ret;
}

int main()
{
	//char str[20] = "12345";
	int ret = my_atoi("0");
	printf("%d", ret);
	return 0;
}